Grokking Tree Coding Patterns for Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Course Overview
Introduction to Tree
Types of Tree
BST Traversal Techniques
BST Operations
Introduction to Level Order Traversal Pattern
Reverse Level Order Traversal (easy)
Solution: Reverse Level Order Traversal
Find Largest Value in Each Tree Row (medium)
Solution: Find Largest Value in Each Tree Row
Maximum Width of Binary Tree (medium)
Solution: Maximum Width of Binary Tree
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Zigzag Traversal (medium)
Solution: Zigzag Traversal
Even Odd Tree (medium)
Solution: Even Odd Tree
N-ary Tree Level Order Traversal (hard)
Solution: N-ary Tree Level Order Traversal
Introduction to Tree Depth Pattern
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Minimum Depth of a Binary Tree (easy)
Solution: Minimum Depth of a Binary Tree
Balanced Binary Tree (easy)
Solution: Balanced Binary Tree
Tree Diameter (easy)
Solution: Tree Diameter
Maximum Depth of N-ary Tree (easy)
Solution: Maximum Depth of N-ary Tree
Find maximum depth of subtree (medium)
Solution: Find maximum depth of subtree
Root to Leaf Path Pattern (medium)
Introduction to Root to Leaf Path Pattern
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Sum of Root To Leaf Binary Numbers (easy)
Solution: Sum of Root To Leaf Binary Numbers
Insufficient Nodes in Root to Leaf Paths (medium)
Solution: Insufficient Nodes in Root to Leaf Paths
Smallest String Starting From Leaf (medium)
Solution: Smallest String Starting From Leaf
Pseudo-Palindromic Paths in a Binary Tree (medium)
Solution: Pseudo-Palindromic Paths in a Binary Tree
All Paths for a Sum (medium)
Solution: All Paths for a Sum
Sum of Path Numbers (medium)
Solution: Sum of Path Numbers
Path With Given Sequence (medium)
Solution: Path With Given Sequence
Count Paths for a Sum (medium)
Solution: Count Paths for a Sum
Leaf Processing Pattern (medium)
Introduction to Leaf Processing Pattern
Sum of Left Leaves (easy)
Solution: Sum of Left Leaves
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Check if all leaves are at same level (medium)
Solution: Check if all leaves are at same level
Delete Leaves With a Given Value (medium)
Solution: Delete Leaves With a Given Value
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
Introduction to Tree View Pattern
Right View of a Binary Tree (medium)
Solution: Right View of a Binary Tree
Top View Of the Binary Tree (medium)
Solution: Top View Of the Binary Tree
Bottom View Of the Binary Tree (medium)
Solution: Bottom View Of the Binary Tree
Boundary of Binary Tree (medium)
Solution: Boundary of Binary Tree
Introduction to Comparison of Two Trees Pattern
Symmetric Tree (easy)
Solution: Symmetric Tree
Mirror Image Trees (medium)
Solution: Mirror Image Trees
Subtree of Another Tree (easy)
Solution: Subtree of Another Tree
Isomorphic Trees (medium)
Solution: Isomorphic Trees
Introduction to Serialize and Deserialize Tree Pattern
Serialize and Deserialize BST (medium)
Solution: Serialize and Deserialize BST
Verify Preorder Serialization of a Binary Tree (medium)
Solution: Verify Preorder Serialization of a Binary Tree
Create Binary Tree From Descriptions (medium)
Solution: Create Binary Tree From Descriptions
Serialize and Deserialize N-ary Tree (hard)
Solution: Serialize and Deserialize N-ary Tree
Solution: Sum of Left Leaves

Problem Statement

Given the root of a binary tree, return the sum of all left leaves.

A leaf is a node that does not have any child nodes, and a left leaf is a leaf that is the left child of its parent.

Examples

Example 1:

  • Input: root = [3,5,10,null,null,8,7]
Image
  • Expected Output: 13
  • Justification: The leaf nodes are 5, 8, and 7, but only 5 and 8 are left leaf nodes. So, sum of left leaf nodes are 5 + 8 = 13.

Example 2:

  • Input: root = [5, 3, 8, 2, 4, null, 6, 1]
Image
  • Expected Output: 1
  • Justification: The only left leaf node is 1 (left child of 2). So, the answer is 1.

Example 3:

  • Input: root = [1, null, 5, null, 4]
Image
  • Expected Output: 0
  • Justification: The tree doesn't have any left leaf node.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

Solution

To solve this problem, we use Depth-First Search (DFS) to traverse the binary tree and find all left leaf nodes. Starting from the root, we recursively explore each node's left and right children. If a node is a leaf (it has no children) and is a left child (tracked using a boolean flag), we add its value to the total sum. For every node, we check its left child by setting the flag as true and its right child with the flag as false. This approach ensures that only left leaf nodes are considered in the sum, and we continue this process until all nodes are visited.

Step-by-Step Algorithm

Start from the root node:

  • Call dfs with the root node and isLeft set to false to indicate it is not a left child.

DFS() Function

  1. Check for a null node:

    • If the current node is null, return 0. This means there are no further nodes in this path.
  2. Identify a left leaf node:

    • If the current node is a leaf (node.left == null && node.right == null) and isLeft is true, return node.val. This adds the value of the left leaf node to the sum.
  3. Recursive traversal for children:

    • Call dfs recursively for the left child (node.left) with isLeft set to true to track it as a left child.
    • Call dfs recursively for the right child (node.right) with isLeft set to false.
  4. Calculate total sum:

    • Return the sum of the results from the left and right child calls to accumulate the total sum of all left leaves in the tree.

Algorithm Walkthrough

Input: root = [3, 5, 10, null, null, 8, 7]:

Image

Start with the root node:

  • The root node is 3. Call dfs(3, false).
  • node is not null, so proceed further.

DFS() Function

  1. Check if root node is a left leaf:

    • node 3 is not a leaf (it has left and right children). Move to the next steps.
  2. Recursive call for the left child of root (3):

    • Call dfs(5, true).
    • node 5 is not null, so proceed further.
    • node 5 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 5 (its value) as it is a left leaf.
  3. Recursive call for the right child of root (3):

    • Call dfs(10, false).
    • node 10 is not null, so proceed further.
    • node 10 is not a leaf node (it has left and right children), so move to the next steps.
  4. Recursive call for the left child of node (10):

    • Call dfs(8, true).
    • node 8 is not null, so proceed further.
    • node 8 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 8 (its value) as it is a left leaf.
  5. Recursive call for the right child of node (10):

    • Call dfs(7, false).
    • node 7 is not null, so proceed further.
    • node 7 is a leaf node, but isLeft is false.
    • Return 0 as it is not a left leaf.
  6. Combine results for node (10):

    • The result for node 10 is dfs(8, true) + dfs(7, false), which is 8 + 0 = 8.
    • Return 8.
  7. Combine results for root node (3):

    • The result for the root node 3 is dfs(5, true) + dfs(10, false), which is 5 + 8 = 13.
    • Return 13.
  8. Final result:

    • The total sum of left leaves in the tree [3,5,10,null,null,8,7] is 13.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • The time complexity of the above solution is , where n is the number of nodes in the binary tree.
  • This is because the algorithm visits each node exactly once in a Depth-First Search (DFS) manner to determine whether it is a left leaf.

Space Complexity

  • The space complexity of the solution is , where h is the height of the binary tree.
  • This is due to the recursion stack used in the DFS. In the worst case, if the tree is completely unbalanced, the recursion stack would go up to , but in a balanced binary tree, it would be .
Sum of Left Leaves (easy)
Leaf-Similar Trees (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity